<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>重置密码</span>
      </div>
      <div class="pwd-box">
        <el-form
          :model="ruleForm"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="原始密码" prop="old_pwd">
            <el-input
              type="password"
              v-model="ruleForm.old_pwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="new_pwd">
            <el-input
              type="password"
              v-model="ruleForm.new_pwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码" prop="re_pwd">
            <el-input
              type="password"
              v-model="ruleForm.re_pwd"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >修改密码</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { resetPasswordApi } from '@/api/index'
export default {
  data () {
    const unRePwd = (rule, value, callback) => {
      if (value === this.ruleForm.old_pwd) {
        callback(new Error('两次密码相同'))
      } else {
        callback()
      }
    }

    const rePwd = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      ruleForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      rules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            trigger: 'blur',
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符'
          }
        ],
        new_pwd: [
          {
            required: true,
            message: '请输入重置密码',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15的非空字符'
          },
          {
            validator: unRePwd,
            trigger: 'blur'
          }
        ],
        re_pwd: [
          {
            validator: rePwd,
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          const { data: res } = await resetPasswordApi(this.ruleForm)
          if (res.code !== 0) {
            return this.$message.error(res.message)
          } else {
            this.$message.success(res.message)
            this.$store.commit('updateToken', '')
            this.$store.commit('updateUserInfo', {})
            return this.$router.push('/login')
          }
        } else {
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.pwd-box {
  width: 400px;
}
</style>
